package com.java.core1.chapter3;
import java.lang.reflect.Field;
import java.util.stream.IntStream;

import org.junit.Assume;
import org.junit.Test;

import junit.framework.Assert;
/**
* @author ClearScenery
* @version 创建时间：2019年3月29日 下午2:13:22
* @ClassName StringTest4
* @Description 
*/
public class StringTest4 {

	/**
	 * 比较字符串相等
	 */
	@Test
	public void testString1() {
		String str1 = "helLo";
		String str2 = "Hello";
		
		System.out.println(str1.equals(str2));
		
		System.out.println(str1.equalsIgnoreCase(str2));
		
		System.out.println(str1.compareTo(str2));
		
	}
	
	
	//空串和null
	@Test
	public void testString2() {
		String str = "";
		if(str != null && str.length()>0) {
			
		}
	}
	
	//码点和代码单元
	@Test
	public void testString3() {
		//java对字符编码进行了包容性的设计，详细请看核心技术卷char类型讲解
		
		String str = "hello";
		//str.codePoints()  会产生一个instream流对象，这是jdk8的新特性
		int[] codePoints = str.codePoints().toArray();
		String str2 = new String(codePoints, 0, codePoints.length);
	
		char[] charArray = str.toCharArray();
		for(char ch: charArray) {
			System.out.println(ch);
		}
		
		String word = "\uD835\uDD46";
		System.out.println("字符为："+word + " String.length(): "+ word.length());
		
		String firstUnit = Integer.toHexString(word.charAt(0));
		String secondUnit = Integer.toHexString(word.charAt(1));
		String codePoint = Integer.toHexString(word.codePointAt(0));
		System.out.println("第一个单元：" + firstUnit + " 第二个：" + secondUnit + " 码点：" +codePoint);
	}
	
	//String API测试
	@Test
	public void testStringApi() {
		String str = "hello";
		
		//返回给定位置的代码单元。除非对底层的代码单元感兴趣，否则不需要调用这个方法
//		Assert.assertEquals('h', str.charAt(0));//过时
		System.out.println(str.charAt(0));
		
		Assume.assumeTrue('h' == str.charAt(0));
		
		//int codePoints() 8.0   返回从给定位置开始的码点。
		IntStream codePoints = str.codePoints();
		int[] array = codePoints.toArray();
		//int offsetByCodePoints(int startlndex, int cpCount) 5.0   返回从 startlndex 代码点开始，位移 cpCount 后的码点索引。
		
		//int compareTo(String other)  按照字典顺序，如果字符串位于 other 之前，返回一个负数；如果字符串位于 other 之后，返回一个正数；如果两个字符串相等，返回 0。
		
		//String trim()  
	}
	
	@Test
	public void testStringSplit() {
		String str = "   ";
		System.out.println(str.length());
	}
	
	@Test
	public void testStringHeap() {
		String str1 = "a";
		String str2 = "b";
		String str3 = str1+str2;
		String str4 = "ab";
		String str5 = "ab";
		String str6 = str1+"b";
		System.out.println(str3==str4);
		System.out.println(str4==str5);
		System.out.println(str6==str5);
	}
	
	@Test
	public void testReflect() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
	    //创建字符串"Hello World"， 并赋给引用s
	    String s = "Hello World"; 

	    System.out.println("s = " + s); //Hello World

	    //获取String类中的value字段
	    Field valueFieldOfString = String.class.getDeclaredField("value");

	    //改变value属性的访问权限
	    valueFieldOfString.setAccessible(true);

	    //获取s对象上的value属性的值
	    char[] value = (char[]) valueFieldOfString.get(s);

	    //改变value所引用的数组中的第5个字符
	    value[5] = '_';

	    System.out.println("s = " + s);  //Hello_World
	
	}
}
